//
//  appmenu4jni.m
//  appmenu4jni
//
//  Created by Wang Zhi on 15/6/7.
//  Copyright (c) 2015年 Wang Zhi. All rights reserved.
//

@import AppKit;

#import "appmenu4jni.h"


static JavaVM *javaVM;
NSMutableDictionary* actionsPool;


@implementation AppMenuAction

-(instancetype)initWithHandler:(jobject)handler {
    self = [self init];
    _handler = handler;
    return self;
}

-(void)handleAction:(id)sender {
    if (_handler != NULL) {
        JavaVM* jvm = javaVM;
        JNIEnv *env;
        
        (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_4);
        jclass cls = (*env)->FindClass(env, "playfx/jni/ActionHandler");
        if (cls == 0) {
            NSLog(@"Not found class");
            return;
        }
        jmethodID method = (*env)->GetMethodID(env, cls, "handleAction", "()V");
        if (method == 0) {
            NSLog(@"Not found method.");
            return;
        }
        
        (*env)->CallVoidMethod(env, _handler, method);
    }
}

@end


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
    JNIEnv *env = 0;
    
    if ((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_4)) {
        return JNI_ERR;
    }
    
    javaVM = jvm;
    
    actionsPool = [[NSMutableDictionary alloc] init];
    
    return JNI_VERSION_1_4;
}


JNIEXPORT void JNICALL Java_playfx_jni_NSAppMenu_insertMenuItem
(JNIEnv *env, jobject obj, jint index, jstring text, jobject handler, jstring key)
{
    NSString *title = [[NSString alloc] initWithCString: (*env)->GetStringUTFChars(env, text, JNI_FALSE)
                       encoding: NSUTF8StringEncoding];
    NSString *keyEq = [[NSString alloc] initWithCString: (*env)->GetStringUTFChars(env, key, JNI_FALSE)
                               encoding: NSUTF8StringEncoding];
    jobject globalRef = (*env)->NewGlobalRef(env, handler);
    AppMenuAction* act = [[AppMenuAction alloc]initWithHandler:globalRef];
    NSMenuItem* item = [[[[NSApp mainMenu] itemAtIndex:0] submenu] insertItemWithTitle:title action:@selector(handleAction:) keyEquivalent:keyEq atIndex:index];
    [item setTarget:act];
    [item setEnabled:YES];
    [actionsPool setObject:act forKey:item];
//    [item setEnabled:true];    // Get the message and print.
//    jclass helloClass = (*env)->GetObjectClass(env, obj);
//    jmethodID getMessageId = (*env)->GetMethodID(env, helloClass, "getMessage", "()Ljava/lang/String;");
//    jstring s = (jstring) (*env)->CallObjectMethod(env, obj, getMessageId);
//    NSLog(@"%s", (*env)->GetStringUTFChars(env, s, JNI_FALSE));
//    [appMenu insertMenuItem:title handler:handler key:keyEq atIndex:index];
}


JNIEXPORT void JNICALL Java_playfx_jni_NSAppMenu_insertSeparator
(JNIEnv *env, jobject obj, jint index)
{
    [[[[NSApp mainMenu] itemAtIndex: 0] submenu] insertItem:[NSMenuItem separatorItem] atIndex: index];
}


JNIEXPORT void JNICALL Java_playfx_jni_NSAppMenu_removeMenuItem
(JNIEnv *env, jobject obj, jint index)
{
    NSMenuItem* menuItem = [[[[NSApp mainMenu] itemAtIndex:0] submenu] itemAtIndex:index];
    if (menuItem != nil) {
        AppMenuAction* action = [actionsPool objectForKey:menuItem];
        if (action != nil) {
            [actionsPool removeObjectForKey:menuItem];
        }
        [[[[NSApp mainMenu] itemAtIndex:0] submenu] removeItem:menuItem];
    }
}

